/**
 * 
 */
package com.yidu.finance.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yidu.finance.model.Member;
import com.yidu.finance.utils.JDBCTools;

/**
 * 描述：会员Dao
 * @author zhouyuxian
 * @date 2019年9月23日 下午6:48:39
 * @ version 1.0
 */
public class MemberDao {
	
	/**
	 * 添加会员
	 * @param member
	 * @return
	 */
	public int insert(Member member){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "insert into member values(null,?,?,?,?,?,?,0)";
			pstm = con.prepareStatement(sql);
			pstm.setString(1, member.getMemberName());
			pstm.setString(2, member.getMemberAccount());
			pstm.setString(3, member.getMemberPassword());
			pstm.setString(4, member.getMemberImage());
			pstm.setInt(5, member.getMemberPhone());
			pstm.setString(6, member.getMemberEmail());
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 按ID删除会员
	 * @param memberID
	 * @return
	 */
	public int delete(int memberID){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "delete from member where member_id=?";
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, memberID);
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 修改会员
	 * @param member
	 * @return
	 */
	public int update(Member member){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "update member set member_name=?,member_account=?,"
					+ "member_password=?,member_image=?,member_phone=?,member_email=?,"
					+ "member_balance=? where member_id=?" ;
			pstm = con.prepareStatement(sql);
			pstm.setString(1, member.getMemberName());
			pstm.setString(2, member.getMemberAccount());
			pstm.setString(3, member.getMemberPassword());
			pstm.setString(4, member.getMemberImage());
			pstm.setInt(5, member.getMemberPhone());
			pstm.setString(6, member.getMemberEmail());
			pstm.setInt(7, member.getMemberBalance());
			pstm.setInt(8, member.getMemberID());
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 查询全部会员
	 * @return
	 */
	public List<Member> selectAll(){
		List<Member> list = new ArrayList<Member>();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = JDBCTools.getCon();
			String sql = "select * from member";
			pstm = con.prepareStatement(sql);
			rs = pstm.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setMemberID(rs.getInt("member_id"));
				member.setMemberName(rs.getString("member_name"));
				member.setMemberAccount(rs.getString("member_account"));
				member.setMemberPassword(rs.getString("member_password"));
				member.setMemberImage(rs.getString("member_image"));
				member.setMemberPhone(rs.getInt("member_phone"));
				member.setMemberEmail(rs.getString("member_email"));
				member.setMemberBalance(rs.getInt("member_balance"));
				list.add(member);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm, rs);
		}
		return list;
	}
	
	/**
	 * 按ID查询会员
	 * @param memberID
	 * @return
	 */
	public Member selectID(int memberID){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = JDBCTools.getCon();
			String sql = "select * from member where member_id=?";
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, memberID);
			rs = pstm.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setMemberID(rs.getInt("member_id"));
				member.setMemberName(rs.getString("member_name"));
				member.setMemberAccount(rs.getString("member_account"));
				member.setMemberPassword(rs.getString("member_password"));
				member.setMemberImage(rs.getString("member_image"));
				member.setMemberPhone(rs.getInt("member_phone"));
				member.setMemberEmail(rs.getString("member_email"));
				member.setMemberBalance(rs.getInt("member_balance"));
				return member;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm, rs);
		}
		return null;
	}
	
	/**
	 * 查询会员的账号和密码
	 * @param name
	 * @param password
	 * @return
	 */
	public Member selectAP(String account,String password){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = JDBCTools.getCon();
			String sql = "select * from member where member_account=? and member_password=?";
			pstm = con.prepareStatement(sql);
			pstm.setString(1, account);
			pstm.setString(2, password);
			rs = pstm.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setMemberID(rs.getInt("member_id"));
				member.setMemberName(rs.getString("member_name"));
				member.setMemberAccount(rs.getString("member_account"));
				member.setMemberPassword(rs.getString("member_password"));
				member.setMemberImage(rs.getString("member_image"));
				member.setMemberPhone(rs.getInt("member_phone"));
				member.setMemberEmail(rs.getString("member_email"));
				member.setMemberBalance(rs.getInt("member_balance"));
				return member;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return null;
	}
}
